﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TFXK.Model;
using TFXK.BLL;
using TFXK.Common;
using log4net;

public partial class admin_LinesManage_Default : BasePage
{
    private static readonly ILog log = LogManager.GetLogger(typeof(admin_LinesManage_Default));
    private static readonly TravelLinesBLL bll = new TravelLinesBLL();
    private static readonly CategoryBLL cbll = new CategoryBLL();
    public string codeNo = "lyxl";

    public string CodeNo
    {
        get
        {
            if (!string.IsNullOrEmpty(Request.QueryString["codeNo"]))
            {
                codeNo = Server.UrlDecode(StringUtil.HtmlEncode(Request.QueryString["codeNo"]));
            }
            return codeNo;
        }
    }

    public DataSet ds = new DataSet();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 设置下拉列表分类信息
    public void SetDDLView()
    {
        DevExpress.Web.ASPxEditors.ListEditItem listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未选择", 0);

        //栏目分类
        DataSet ds = cbll.GetNextNodeByCode(CodeNo);
        ddlMsgType.DataSource = ds.Tables[0].DefaultView;
        ddlMsgType.TextField = "title";
        ddlMsgType.ValueField = "id";
        ddlMsgType.DataBind();
        ddlMsgType.Items.Insert(0, listItemQXZ);
        ddlMsgType.SelectedIndex = 0;

        listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未选择", 0);
        //栏目分类
        ds = cbll.GetNextNodeByCode("lytjlb");
        ddlTravelShowType.DataSource = ds.Tables[0].DefaultView;
        ddlTravelShowType.TextField = "title";
        ddlTravelShowType.ValueField = "id";
        ddlTravelShowType.DataBind();
        ddlTravelShowType.Items.Insert(0, listItemQXZ);
        ddlTravelShowType.SelectedIndex = 0;

        listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未选择", 0);
        //栏目分类
        ds = cbll.GetNextNodeByCode("ztly");
        ddlSubject.DataSource = ds.Tables[0].DefaultView;
        ddlSubject.TextField = "title";
        ddlSubject.ValueField = "id";
        ddlSubject.DataBind();
        ddlSubject.Items.Insert(0, listItemQXZ);
        ddlSubject.SelectedIndex = 0;

        listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未选择", -1);
        ddlState.Items.Insert(0, listItemQXZ);
        listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未审核", 0);
        ddlState.Items.Insert(1, listItemQXZ);
        listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("已审核", 1);
        ddlState.Items.Insert(2, listItemQXZ);
        ddlState.SelectedIndex = 0;

    }
    #endregion

    #region 分页绑定
    private void InitData(int PageIndex)
    {
        this.Title = "列表";    // 设置标题
        string strTemp = string.Empty;
        int intTemp = 0;
        DataSet ds = new DataSet();
        int recordcount = 0; //获取总条数
        string where = "1=1";
        #region 查询过滤

        if (!string.IsNullOrEmpty(Request.QueryString["title"]))
        {
            strTemp = Server.UrlDecode(StringUtil.HtmlEncode(Request.QueryString["title"]));
            if (!string.IsNullOrEmpty(strTemp))
            {
                where += " and title like '%" + strTemp + "%'";
                txtSearchName.Text = strTemp;

            }
            strTemp = "";
        }

        if (!string.IsNullOrEmpty(Request.QueryString["travelType"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["travelType"] + "");
            }
            catch
            {
                intTemp = 0;
            }

            if (intTemp > 0)
            {
                where += " and travelType=" + intTemp;   
            }
            intTemp = 0;
            
        }

        if (!string.IsNullOrEmpty(Request.QueryString["subjectId"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["subjectId"] + "");
            }
            catch
            {
                intTemp = 0;
            }
         
            if (intTemp > 0)
            {
                where += " and subjectId=" + intTemp; 
            }
            intTemp = 0;
        }

        if (!string.IsNullOrEmpty(Request.QueryString["travelShowType"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["travelShowType"] + "");
            }
            catch
            {
                intTemp = 0;
            }
         
            if (intTemp > 0)
            {
                where += " and travelShowType=" + intTemp;  
            }
            intTemp = 0;
        }


        if (!string.IsNullOrEmpty(Request.QueryString["isTuiJian"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isTuiJian"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isTuiJian=" + intTemp;
                chkTuiJian.Checked = true;
            }
            intTemp = 0;
        }



        if (!string.IsNullOrEmpty(Request.QueryString["isTeJia"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isTeJia"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isTeJia=" + intTemp;
                chkTeJia.Checked = true;
            }
            intTemp = 0;
        }


        if (!string.IsNullOrEmpty(Request.QueryString["isSNHot"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isSNHot"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isSNHot=" + intTemp;
                chkIsSNHot.Checked = true;
            }
            intTemp = 0;
        }




        if (!string.IsNullOrEmpty(Request.QueryString["isGNHot"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isGNHot"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isGNHot=" + intTemp;
                chkIsGNHot.Checked = true;
            }
            intTemp = 0;
        }

        if (!string.IsNullOrEmpty(Request.QueryString["isZBTravel"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isZBTravel"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isZBTravel=" + intTemp;
                chkIsZBTravel.Checked = true;
            }
            intTemp = 0;
        }


        if (!string.IsNullOrEmpty(Request.QueryString["isCJTTuiJian"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isCJTTuiJian"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isCJTTuiJian=" + intTemp;
                chkCJTuiJian.Checked = true;
            }
            intTemp = 0;
        }


        if (!string.IsNullOrEmpty(Request.QueryString["isShowHH"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isShowHH"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isShowHH=" + intTemp;
                chkIsShowHH.Checked = true;
            }
            intTemp = 0;
        }


        if (!string.IsNullOrEmpty(Request.QueryString["isShowJD"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["isShowJD"] + "");
            }
            catch
            {
                intTemp = 0;
            }
            if (intTemp > 0)
            {
                where += " and isShowJD=" + intTemp;
                chkIsShowJD.Checked = true;
            }
            intTemp = 0;
        }
        


        if (!string.IsNullOrEmpty(Request.QueryString["theState"]))
        {
            try
            {
                intTemp = int.Parse(Request.QueryString["theState"] + "");
            }
            catch
            {
                intTemp = -1;
            }
            if (intTemp > -1)
            {
                where += " and theState=" + intTemp;
            }
            intTemp = 0;
        }


        #endregion


        ds = bll.GetAllListByCodeNo(AspNetPager1.PageSize, PageIndex, CodeNo, where, out recordcount);

        AspNetPager1.RecordCount = recordcount;

        this.rptBind.DataSource = ds.Tables[0].DefaultView;
        this.rptBind.DataBind();
    }
    #endregion

    #region 分页控件加载
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                SetDDLView();
                InitData(e.NewPageIndex);
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 删除
    protected void ibtnDel_OnCommand(object sender, CommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandName);
        try
        {
            // 防止网页重复提交
            if (this.CheckRefreshed1.isRefreshed == false)
            {
                bll.Delete(id);
                Msg.Show("删除成功!");
            }
            InitData(1); //重新绑定
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 多选删除
    protected void lbtnDelAll_Click(object sender, EventArgs e)
    {
        try
        {
            // 防止网页重复提交
            if (this.CheckRefreshed1.isRefreshed == false)
            {
                string id = string.Empty;
                bool flg = false;

                foreach (RepeaterItem rptItem in this.rptBind.Items)
                {
                    HtmlInputCheckBox chkDelete = rptItem.FindControl("chkSelected") as HtmlInputCheckBox;
                    HiddenField hf = rptItem.FindControl("hiddenParID") as HiddenField;
                    if (chkDelete.Checked == true)
                    {
                        id = chkDelete.Value.ToString();
                        bll.Delete(Int32.Parse(id));
                        flg = true;
                    }
                }

                if (flg)
                {
                    Msg.Show("删除成功!");
                }
                else
                {
                    Msg.Show("请选择要删除的记录!");
                }
            }
            InitData(1); //重新绑定
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 修改
    protected void lbtnModify_Click(object sender, EventArgs e)
    {
        try
        {
            string id = string.Empty;

            foreach (RepeaterItem rptItem in this.rptBind.Items)
            {
                HtmlInputCheckBox chkDelete = rptItem.FindControl("chkSelected") as HtmlInputCheckBox;
                if (chkDelete.Checked == true)
                {
                    id = chkDelete.Value.ToString();
                    Response.Redirect("Action.aspx?type=modify&id=" + id + "&codeNo=" + CodeNo, false);
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 搜索按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            string title = this.txtSearchName.Text;
            string travelType = this.ddlMsgType.SelectedItem.Value.ToString();
            string subjectId = this.ddlSubject.SelectedItem.Value.ToString(); //主题
            string travelShowType = this.ddlTravelShowType.SelectedItem.Value.ToString(); //旅游分类
            int isTuiJian =chkTuiJian.Checked ? 1 : 0;
            int isTeJia = chkTeJia.Checked ? 1 : 0;
            int isSNHot = chkIsSNHot.Checked ? 1 : 0;
            int isGNHot = chkIsGNHot.Checked ? 1 : 0;
            int isZBTravel = chkIsZBTravel.Checked ? 1 : 0;
            int isCJTTuiJian = chkCJTuiJian.Checked ? 1 : 0;
            int isShowHH = chkIsShowHH.Checked ? 1 : 0;
            int isShowJD = chkIsShowJD.Checked ? 1 : 0;
            string theState = this.ddlState.SelectedItem.Value.ToString(); //状态

            Response.Redirect("Default.aspx?title=" + title + "&travelType=" + travelType
                + "&subjectId=" + subjectId + "&travelShowType=" +
                travelShowType + "&isTuiJian=" + isTuiJian
                + "&isTeJia=" + isTeJia + "&isSNHot="
                + isSNHot + "&isGNHot=" + isGNHot
                + "&isZBTravel=" + isZBTravel + "&isCJTTuiJian="
                + isCJTTuiJian + "&isShowHH=" + isShowHH + "&theState=" + theState + "&isShowJD=" + isShowJD, false);
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 设置分类信息
    public string GetCategoryName(object obj)
    {
        try
        {
            CategoryBLL cbll = new CategoryBLL();
            return cbll.GetModel(int.Parse(obj.ToString())).title;
        }
        catch
        {
            return "";
        }
    }
    #endregion
}
